﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.Model;
using MVCClubSite.IDAL;
using System.Data.Linq;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL DAL for user profiles
    /// </summary>
    public class UserProfiles : IUserProfiles
    {
        DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public UserProfiles()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public UserProfiles(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        public IQueryable<UserProfile> GetUserProfiles()
        {
            return from p in _db.cs_UserProfiles
                   select new UserProfile
                   {
                       UserID = p.UserID,
                       FirstName = p.FirstName,
                       LastName = p.LastName,
                       Address = p.Address,
                       Phone = p.Phone,
                       Bio = p.Bio,
                       Signature = p.Signature,
                       Avatar = p.Avatar == null ? p.Avatar.ToArray() : null,
                       Newsletter = p.Newsletter,
                       LastUpdate = p.LastUpdate,
                       LastActivity = p.LastActivity
                   };
        }

        public UserProfile GetUserProfile(Guid userID)
        {
            cs_UserProfile dbUserProfile = _db.cs_UserProfiles.Where(up => up.UserID == userID).SingleOrDefault();
            
            UserProfile result = new UserProfile();
            result.UserID = userID;

            if (dbUserProfile != null)
            {
                result.UserID = dbUserProfile.UserID;
                result.FirstName = dbUserProfile.FirstName;
                result.LastName = dbUserProfile.LastName;
                result.Address = dbUserProfile.Address;
                result.Phone = dbUserProfile.Phone;
                result.Bio = dbUserProfile.Bio;
                result.Signature = dbUserProfile.Signature;

                if (dbUserProfile.Avatar != null && dbUserProfile.Avatar.Length > 0)
                    result.Avatar = dbUserProfile.Avatar.ToArray();
                else
                    result.Avatar = null;

                result.Newsletter = dbUserProfile.Newsletter;
                result.LastUpdate = dbUserProfile.LastUpdate;
                result.LastActivity = dbUserProfile.LastActivity;
            }

            return result;
        }

        public void Save(UserProfile userProfile)
        {
            using (DB db = new DB())
            {
                cs_UserProfile dbUserProfile = db.cs_UserProfiles.Where(up => up.UserID == userProfile.UserID).SingleOrDefault();
                bool isNew = false;
                if (isNew)
                {
                    dbUserProfile = new cs_UserProfile();
                    isNew = false;
                }

                dbUserProfile.FirstName = userProfile.FirstName;
                dbUserProfile.LastName = userProfile.LastName;
                dbUserProfile.Address = userProfile.Address;
                dbUserProfile.Phone = userProfile.Phone;
                dbUserProfile.Bio = userProfile.Bio;
                dbUserProfile.Signature = userProfile.Signature;

                if (userProfile.Avatar != null)
                    dbUserProfile.Avatar = userProfile.Avatar;
                else
                    dbUserProfile.Avatar = new Binary(new byte[] { }); // empty byte

                dbUserProfile.Newsletter = userProfile.Newsletter;
                dbUserProfile.LastUpdate = userProfile.LastUpdate;
                dbUserProfile.LastActivity = userProfile.LastActivity;

                if (isNew)
                    db.cs_UserProfiles.InsertOnSubmit(dbUserProfile);

                db.SubmitChanges();
            }            
        }

        public void Delete(Guid userID)
        {
            cs_UserProfile dbUserProfile = _db.cs_UserProfiles.Where(up => up.UserID == userID).SingleOrDefault();

            if (dbUserProfile != null)
            {
                _db.cs_UserProfiles.DeleteOnSubmit(dbUserProfile);
                _db.SubmitChanges();
            }
        }
    }
}
